package capitulo4;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.AffineTransform;

@SuppressWarnings("serial")
public class RotatePolygon extends Applet implements KeyListener, MouseListener, MouseMotionListener {

	private int[] xPoints = {0,-10,-7,7,10};
	private int[] yPoints = {-10,-2,10,10,-2};

	// Here's the shape used for drawing
	private Polygon poly;
	
	// Polygon rotation variable
	int rotation = 0;
	
	//applet init event
	public void init(){
		poly= new Polygon(xPoints,yPoints,xPoints.length);	
		
		// initialize the listeners
		this.addKeyListener(this);
		this.addMouseListener(this);	
		this.addMouseMotionListener(this);
	} 
	
	// Applet paint event
	public void paint(Graphics g){
		// Create an instance of Graphics2D
		Graphics2D g2d = (Graphics2D)g;
		
		// save the identity transform
		AffineTransform identity = new AffineTransform();
		
		// Save the window width/height
		int width = getSize().width;
		int height = getSize().height;
		
		// Fill the background with black
		g2d.setColor(Color.BLACK);
		g2d.fillRect(0, 0, width, height);
		
		
		// Reset Graphics2D to the identity transform
		g2d.setTransform(identity);
			
		// Move, rotate and scale the shape randomly
		g2d.translate(width/2, height/2);
		g2d.scale(20, 20);
		g2d.rotate(Math.toRadians(rotation));
			
		// Draw the shape wiht a random color
		g2d.setColor(Color.RED);
		g2d.fill(poly);
		g2d.setColor(Color.BLUE);
		g2d.draw(poly);
		
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent m) {
		switch(m.getButton()){
		case MouseEvent.BUTTON1:
			rotation--;
			if (rotation <0) rotation = 359;
			repaint();
			break;
		case MouseEvent.BUTTON3:
			rotation++;
			if (rotation > 360) rotation = 0;
			repaint();
			break;
		}
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyPressed(KeyEvent k) {
		switch(k.getKeyCode()){
		case KeyEvent.VK_LEFT:
			rotation--;
			if (rotation <0) rotation = 359;
			repaint();
			break;
		case KeyEvent.VK_RIGHT:
			rotation++;
			if (rotation > 360) rotation = 0;
			repaint();
			break;
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseMoved(MouseEvent m) {
		int middle = getSize().width/2;
		int size = m.getX()/middle;

		if (size==0){
			rotation--;
			if (rotation <0) rotation = 359;
			repaint();
		} else if (size==1){
			rotation++;
			if (rotation > 360) rotation = 0;
			repaint();
		}
		
	}
	
}
